package com.guojunyu.exp2.conf;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;


@Component
public class CustomEventMulticaster {

    @Bean
    ApplicationEventMulticaster applicationEventMulticaster(ThreadPoolTaskExecutor taskExecutor) {
        SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
        eventMulticaster.setTaskExecutor(taskExecutor);
        return eventMulticaster;
    }


    static public class NoticeEvent extends ApplicationEvent {

        private final String message;

        private static final Logger logger = LoggerFactory.getLogger(NoticeEvent.class);

        public NoticeEvent(String message) {
            super(message);
            this.message = message;
        }

        public String getMessage() {
            return message;
        }
    }

    @Component
    static public class NoticeListener implements ApplicationListener<NoticeEvent> {

        private static final Logger logger = LoggerFactory.getLogger(NoticeListener.class);

        @Override
        public void onApplicationEvent(NoticeEvent noticeEvent) {
            logger.info("监听到NoticeEvent,睡眠线程2秒");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            logger.info("noticeEvent的属性message是:" + noticeEvent.getMessage());
        }

    }


}
